package com.lidong.producer.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 创建服务
 */
@RestController
public class EurekaProducerController {

    @Value("${server.port}")
    private Integer port;

    /**
     * 服务接口
     * @param name
     * @return
     */
    @HystrixCommand(fallbackMethod = "testError",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
    })
    @GetMapping("/hello")
    public String sayHello(@RequestParam("name")String name) {
        try {
            TimeUnit.MILLISECONDS.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String result = "hello ---> "+name+" port -->"+port;
        System.out.println(result);
        return result;
    }

    public String testError(String dd) {
        System.out.println("服务提供者超时了");
        return "服务提供者超时了";
    }


    /**
     * 服务接口
     * @param
     * @return
     */
    @RequestMapping("/hello2")
    public String sayHello2() {
        return "hello2 ---> ";
    }


    @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
    public String echo(@PathVariable String string) {
        return "hello Nacos Discovery " + string;
    }

    @RequestMapping(value = "/divide", method = RequestMethod.GET)
    public String divide(@RequestParam Integer a, @RequestParam Integer b) {
        return String.valueOf(a / b);
    }


}
